import {
    COURT_DATE_GET_REQUEST,
    COURT_DATE_GET_SUCCESS,
    COURT_DATE_GET_FAILURE,

    COURT_FIELD_GET_REQUEST,
    COURT_FIELD_GET_SUCCESS,
    COURT_FIELD_GET_FAILURE,

    COURT_FIELD_SELECTED,
    COURT_FIELD_UNSELECTED,
    COURT_FIELD_CLEAR,

    SET_LIST_LOADING_STATUS
} from '../mutation-types';

//导入courtService 
import courtService from '../../services/courtService';


const getCourtDate = ({ dispatch }, court) => {
    dispatch(COURT_DATE_GET_REQUEST);
    return courtService.GetCourtDate(court).then(res => {
        if (res.ok) {
            dispatch(COURT_DATE_GET_SUCCESS, res.data, court);
        } else {
            dispatch(COURT_DATE_GET_FAILURE);
        }
    });
}

const getCourtFields = ({ dispatch }, court, date) => {
    dispatch(SET_LIST_LOADING_STATUS, true);
    dispatch(COURT_FIELD_GET_REQUEST);
    return courtService.GetCourtFields(court, date).then(res => {
        if (res.ok) {
            dispatch(COURT_FIELD_GET_SUCCESS, res.data, court, date);
            dispatch(SET_LIST_LOADING_STATUS, false);
        } else {
            dispatch(COURT_FIELD_GET_FAILURE);
            dispatch(SET_LIST_LOADING_STATUS, false);
        }
    });
}


const selectCourtField = ({ dispatch }, fieldId) => { dispatch(COURT_FIELD_SELECTED, fieldId) }
const unSelectCourtField = ({ dispatch }, fieldId) => { dispatch(COURT_FIELD_UNSELECTED, fieldId) }

//导出 ,dispatch 由 mutation监听
export default {
    getCourtDate,
    getCourtFields,
    selectCourtField,
    unSelectCourtField
}
